package com.example.arithmeticleetcode.leetcode.october;

import java.util.*;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-10-21 09:37
 **/
public class Demo10 {

    public static void main(String[] args) {

        //太笨的我没有做出来
        int[][] towers1 = {{1, 2, 5}, {2, 1, 7}, {3, 1, 9}};
        System.out.println(Arrays.toString(bestCoordinate(towers1, 2)));

        int[][] towers2 = {{23, 11, 21}};
        System.out.println(Arrays.toString(bestCoordinate(towers2, 9)));


        int[][] towers3 = {{1, 2, 13}, {2, 1, 7}, {0, 1, 9}};
        System.out.println(Arrays.toString(bestCoordinate(towers3, 2)));


        int[][] towers4 = {{2, 1, 9}, {0, 1, 9}};
        System.out.println(Arrays.toString(bestCoordinate(towers4, 2)));

        int[][] towers5 = {{42, 0, 0}};
        System.out.println(Arrays.toString(bestCoordinate(towers5, 2)));

        int[][] towers6 = {{0, 1, 2}, {2,1,2}, {1,0,2}, {1,2,2}};
        System.out.println(Arrays.toString(bestCoordinate(towers6, 1)));
    }

    public static int[] bestCoordinate(int[][] towers, int radius) {
        if (towers.length == 1) {
            if (towers[0][2] == 0) {
                return new int[] {0, 0};
            }
            return new int[]{towers[0][0], towers[0][1]};
        }
        Map<Integer, List<int[]>> map = new HashMap<>();
        for (int i = 0, len = towers.length; i < len; i++) {
            int x1 = towers[i][0], y1 = towers[i][1], q1 = towers[i][2];
            int sum = q1;
            for (int j = 0; j < len; j++) {
                if (i != j) {
                    int xj = towers[j][0], yj = towers[j][1], qj = towers[j][2];
                    double d = Math.sqrt(Math.pow((xj - x1), 2) + Math.pow((yj - y1), 2));
                    if (d <= radius) {
                        sum += qj / (1 + d);
                    }
                }
            }
            if (map.containsKey(sum)) {
                List<int[]> list = map.get(sum);
                list.add(new int[]{x1, y1});
            } else {
                List<int[]> list = new ArrayList<>();
                list.add(new int[]{x1, y1});
                map.put(sum, list);
            }
        }

        int max = Integer.MIN_VALUE;
        List<int[]> list = new ArrayList<>();
        for (Map.Entry<Integer, List<int[]>> integerListEntry : map.entrySet()) {
            if (max < integerListEntry.getKey()) {
                max = integerListEntry.getKey();
                list = integerListEntry.getValue();
            }
        }
        Double min = Double.MAX_VALUE;
        int[] arr = new int[2];
        for (int[] ints : list) {
            double d = Math.sqrt(Math.pow((ints[0]), 2) + Math.pow((ints[1]), 2));
            if (d < min) {
                min = d;
                arr = ints;
            }
        }
        return arr;
    }
}
